package com.kangfawei.io;

import java.io.*;
import java.util.UUID;

/**
 * IO第一个程序
 * @author 康发伟
 *
 * 1、创建源
 * 2、选择流
 * 3、操作
 * 4、释放资源
 */
public class FileCopy {
     public static void main(String[] args) {
        copyFileByReadAndWrite();
    }

    public static void copyTest01(){
        // 创建源
        File file = new File("E:/IO_src/test01.txt");
        String newPath = "E:/IO_target/test01."+getSuffix(file);
        File newFile = new File(newPath);

        InputStream is = null;
        OutputStream os = null;
        try {
            // 选择流
            is = new FileInputStream(file);
            os = new FileOutputStream(newFile);
            byte[] bytes = new byte[1024];
            int temp = 0;
            while ((temp = is.read(bytes)) != (-1)){
                os.write(bytes,0,bytes.length);
                os.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if(is != null){
                    is.close();
                }
                if(os !=null) {
                    os.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void copyTest02(){
        String path = "E:/IO_src/test02.jpg";
        File file = new File(path);
        File newFile = new File("E:/IO_target/"+generateFileName(file));

        InputStream is = null;
        OutputStream os = null;

        try {
            is = new FileInputStream(file);
            os = new FileOutputStream(newFile);
            byte[] bytes = new byte[1024];
            int temp = 0;
            while((temp = is.read(bytes)) != -1){
                os.write(bytes);
            }
            os.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if(is!=null){
                    is.close();
                }
                if(os != null ){
                    os.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 通过try...with...resource自动释放资源
     */
    public static void copyTest03(){
        String path = "E:/IO_src/test02.jpg";
        File file = new File(path);
        File newFile = new File("E:/IO_target/"+generateFileName(file));

        // try...with...resource
        try (InputStream is = new FileInputStream(file);
             OutputStream os = new FileOutputStream(newFile);){

            byte[] bytes = new byte[1024];
            int temp = 0;
            while((temp = is.read(bytes)) != -1){
                os.write(bytes);
            }
            os.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    //  文件字符流
    // 只能针对字符文件
    public static void copyFileByReadAndWrite(){
        // 创建源
        File file = new File("E:/IO_src/test01.txt");
        String newPath = "E:/IO_target/test01."+getSuffix(file);
        File newFile = new File(newPath);

        Reader reader = null;
        Writer writer = null;
        try {
            // 选择流
            reader = new FileReader(file);
            writer = new FileWriter(newFile);

            int temp = 0;
            while ((temp = reader.read()) != (-1)){
                writer.write(temp);
                writer.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if(reader != null){
                    reader.close();
                }
                if(writer !=null) {
                    writer.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static String getSuffix(File file){

        String path = file.getAbsolutePath();
        return path.substring(path.lastIndexOf(".")+1);
    }

    public static String generateFileName(File file){
        String uuid = UUID.randomUUID().toString();
        String fileName = uuid+"."+getSuffix(file);
        System.out.println(fileName);
        return fileName;
    }
}
